package com.calmcar.adas.serverman;

import android.content.Context;
import android.util.Log;

import com.calmcar.adas.apiserver.AdasConf;
import com.calmcar.adas.apiserver.model.AdasPoint;
import com.calmcar.adas.apiserver.model.LdwDetectInfo;
import com.calmcar.adas.dao.CalmCarLdwData;

import java.util.List;


/**
 * Created by lelexiao on 2018/5/4.
 */

public class LdwServerManager {

    Context  mContext;
    public LdwServerManager(Context mContext) {
        this.mContext = mContext;
    }



    public LdwDetectInfo getLdwDetectInfo() {
        return ldwDetectInfo;
    }

    public void setLdwDetectInfo(LdwDetectInfo ldwDetectInfo) {
        this.ldwDetectInfo = ldwDetectInfo;
    }

    LdwDetectInfo ldwDetectInfo;
    long lastTime=0;
    public void laneDataCallBack(CalmCarLdwData calmCarLdwData ) {
        LdwDetectInfo ldwDetectInfoTemp=laneDataConvert( calmCarLdwData  );
                if(ldwDetectInfoTemp !=null){
                    lastTime=System.currentTimeMillis();
                    ldwDetectInfo=ldwDetectInfoTemp ;
                }else{
                    if(System.currentTimeMillis()-lastTime> 5 ){
                        ldwDetectInfo=null;
                    }
                }
    }

//
//    public void sendFrameData(CvCameraViewFrame inputStream, double rate ){
//        if(AdasConf.LANE_DETECT_OPEN  ){
//            if(rate> Constant.CAR_SPEED_LANE_THRESHOLD) {
//                adasServicePool.sendLaneData(inputStream);
//            } else{
//                setLdwDetectInfo(null);
//            }
//        }else{
//            setLdwDetectInfo(null);
//        }
//    }



//    public LdwDetectInfo laneDetectProcessForCameraView (CvCameraViewFrame inputStream, double rate ){
//        if(AdasConf.LANE_DETECT_OPEN) {
//            if(rate> Constant.CAR_SPEED_LANE_THRESHOLD) {
//                ldwDetectInfo = ldwServer.laneDetectProcessForCameraView(inputStream);
//                return ldwDetectInfo;
//            } else{
//                setLdwDetectInfo(null);
//                return null;
//            }
//        }else{
//            setLdwDetectInfo(null);
//            return null;
//        }
//    }


//    public int  putHsml(String hsmlHead,String hsml){
//         return ldwServer.putHsml(hsmlHead,hsml);
//    }

//    /**
//     * 设置视野焦点
//     * @param mv_x
//     * @param mv_y
//     */
//    public void setVPPara (int mv_x,int mv_y) {
//        ldwServer. setVPParaNew ( mv_x,mv_y);
//    }




    public LdwDetectInfo laneDataConvert(CalmCarLdwData laneinfoBean ){
        LdwDetectInfo ldwDetectInfo=new LdwDetectInfo();
        int sDepartureOrientation ;
        int sLaneDetectionResult ;
        try {
            List<CalmCarLdwData.CalmCarPoint> curZone = laneinfoBean.getCurrentZone();
            sDepartureOrientation = laneinfoBean.getSDepartureOrientation();
            sLaneDetectionResult = laneinfoBean.getSLaneDetectionResult();
            ldwDetectInfo.setDetectState(sDepartureOrientation);
            ldwDetectInfo.setDetectResult( sLaneDetectionResult);

            ldwDetectInfo.setVpy0(laneinfoBean.getVpy0());
            ldwDetectInfo.setVpy1(laneinfoBean.getVpy1());

            Log.e("vp",laneinfoBean.getVpy0()+","+laneinfoBean.getVpy1() + "," +"******************");

            switch(sDepartureOrientation ){
                case -1:
                    ldwDetectInfo.setLdwType(0);
                    for (int j=1;j<curZone.size();j++){
                        ldwDetectInfo.lineRect[j-1] = new AdasPoint(curZone.get(j).getX(), curZone.get(j).getY());
                    }
                    break;
                case 0:
                    ldwDetectInfo.setLdwType(-1);
                    break;
                case 1://正常
                    for (int j=1;j<curZone.size();j++){
                        ldwDetectInfo.lineRect[j-1]  =  new AdasPoint(curZone.get(j).getX(), curZone.get(j).getY());
                    }
                    ldwDetectInfo.setLdwType(1);
                    if (sLaneDetectionResult==2 || sLaneDetectionResult==3 || sLaneDetectionResult==6 || sLaneDetectionResult==7){
                        if((ldwDetectInfo.lineRect[0].getY()==ldwDetectInfo.lineRect[5].getY())&&(ldwDetectInfo.lineRect[1].getY()==ldwDetectInfo.lineRect[4].getY())&&(ldwDetectInfo.lineRect[2].getY()==ldwDetectInfo.lineRect[3].getY())){
                            if ((ldwDetectInfo.lineRect[2].getX()< AdasConf.centerX )&&ldwDetectInfo.lineRect[3].getX() > AdasConf.centerX ){
                                if (ldwDetectInfo.lineRect[0].getY()<ldwDetectInfo.lineRect[1].getY() && ldwDetectInfo.lineRect[1].getY()<ldwDetectInfo.lineRect[2].getY() &&ldwDetectInfo.lineRect[5].getY()<ldwDetectInfo.lineRect[4].getY()&&ldwDetectInfo.lineRect[4].getY()<ldwDetectInfo.lineRect[3].getY())
                                    ldwDetectInfo.setLdwType(1);
                            }
                        }
                    }
                    break;
                case 2://左偏
                    for (int j=1;j< curZone.size();j++){
                        ldwDetectInfo.lineRect[j-1]  = new AdasPoint(curZone.get(j).getX(), curZone.get(j).getY());
                    }
                    ldwDetectInfo.setLdwType(2);
                    if (sLaneDetectionResult==2 || sLaneDetectionResult==3 || sLaneDetectionResult==6 || sLaneDetectionResult==7){
                        if((ldwDetectInfo.lineRect[0].getY()==ldwDetectInfo.lineRect[5].getY())&&(ldwDetectInfo.lineRect[1].getY()==ldwDetectInfo.lineRect[4].getY())&&(ldwDetectInfo.lineRect[2].getY()==ldwDetectInfo.lineRect[3].getY())){
                            if ((ldwDetectInfo.lineRect[2].getX()< AdasConf.centerX )&&ldwDetectInfo.lineRect[3].getX() > AdasConf.centerX ){
                                if (ldwDetectInfo.lineRect[0].getY()<ldwDetectInfo.lineRect[1].getY() && ldwDetectInfo.lineRect[1].getY()<ldwDetectInfo.lineRect[2].getY() &&ldwDetectInfo.lineRect[5].getY()<ldwDetectInfo.lineRect[4].getY()&&ldwDetectInfo.lineRect[4].getY()<ldwDetectInfo.lineRect[3].getY())
                                    ldwDetectInfo.setLdwType(2);
                            }
                        }
                    }
                    break;
                case 3://右偏
                    for (int j=1;j<curZone.size();j++){
                        ldwDetectInfo.lineRect[j-1]  =  new AdasPoint(curZone.get(j).getX(), curZone.get(j).getY());
                    }
                    ldwDetectInfo.setLdwType(3);
                    if (sLaneDetectionResult==2 || sLaneDetectionResult==3 || sLaneDetectionResult==6 || sLaneDetectionResult==7){
                        if((ldwDetectInfo.lineRect[0].getY()==ldwDetectInfo.lineRect[5].getY())&&(ldwDetectInfo.lineRect[1].getY()==ldwDetectInfo.lineRect[4].getY())&&(ldwDetectInfo.lineRect[2].getY()==ldwDetectInfo.lineRect[3].getY())){
                            if ((ldwDetectInfo.lineRect[2].getX()< AdasConf.centerX )&&ldwDetectInfo.lineRect[3].getX() > AdasConf.centerX ){
                                if (ldwDetectInfo.lineRect[0].getY()<ldwDetectInfo.lineRect[1].getY() && ldwDetectInfo.lineRect[1].getY()<ldwDetectInfo.lineRect[2].getY() &&ldwDetectInfo.lineRect[5].getY()<ldwDetectInfo.lineRect[4].getY()&&ldwDetectInfo.lineRect[4].getY()<ldwDetectInfo.lineRect[3].getY())
                                    ldwDetectInfo.setLdwType(3);
                            }
                        }
                    }
                    break;
            }
        } catch (Exception e) {
            ldwDetectInfo.setLdwType(-1);
        }
        return ldwDetectInfo;
    }


}
